package com.pan.kang.transfer;

import com.pan.kang.entity.TableSchemaEntity;
import com.pan.kang.model.Request;
import com.pan.kang.model.TableSchema;
import com.pan.kang.model.TableSchemaExcel;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

@Mapper(componentModel = "spring")
public abstract class TableSchemaTransfer {

    public List<TableSchemaEntity> modelToEntity(TableSchema model) {
        List<TableSchemaEntity> tableSchemaEntityList = new LinkedList<>();


        List<TableSchema.Field> fieldList = model.getFieldList();
        for (TableSchema.Field field : fieldList) {
            TableSchemaEntity entity = new TableSchemaEntity();
            entity.setDbName(model.getDbName());
            entity.setTableName(model.getTableName());
            entity.setTableComment(model.getTableComment());
            entity.setMockNum(model.getMockNum());
            entity.setFieldName(field.getFieldName());
            entity.setFieldType(field.getFieldType());
            entity.setDefaultValue(field.getDefaultValue());
            entity.setNotNull(field.isNotNull());
            entity.setComment(field.getComment());
            entity.setPrimaryKey(field.isPrimaryKey());
            entity.setAutoIncrement(field.isAutoIncrement());
            entity.setMockType(field.getMockType());
            entity.setMockParams(field.getMockParams());
            entity.setOnUpdate(field.getOnUpdate());
            tableSchemaEntityList.add(entity);
        }

        return tableSchemaEntityList;
    }

    public TableSchema requestToModel(Request.TableSchemaRequest request) {
        TableSchema model = new TableSchema();
        model.setDbName(request.getDbName());
        model.setTableName(request.getTableName());
        model.setTableComment(request.getTableComment());
        model.setMockNum(request.getMockNum());

        List<TableSchema.Field> fieldList = new ArrayList<>();
        model.setFieldList(fieldList);

        for (Request.TableSchemaRequest.Field requestField : request.getFieldList()) {
            TableSchema.Field field = new TableSchema.Field();
            field.setFieldName(requestField.getFieldName());
            field.setFieldType(requestField.getFieldType());
            field.setFieldName(requestField.getFieldName());
            field.setFieldType(requestField.getFieldType());
            field.setDefaultValue(requestField.getDefaultValue());
            field.setNotNull(requestField.isNotNull());
            field.setComment(requestField.getComment());
            field.setPrimaryKey(requestField.isPrimaryKey());
            field.setAutoIncrement(requestField.isAutoIncrement());
            field.setMockType(requestField.getMockType());
            field.setMockParams(requestField.getMockParams());
            field.setOnUpdate(requestField.getOnUpdate());
            fieldList.add(field);
        }
        return model;
    }

    public TableSchema entityToModel(List<TableSchemaEntity> list) {
        TableSchema model = new TableSchema();
        List<TableSchema.Field> fieldList = new ArrayList<>();
        model.setFieldList(fieldList);
        for (TableSchemaEntity entity : list) {
            model.setDbName(entity.getDbName());
            model.setTableName(entity.getTableName());
            model.setTableComment(entity.getTableComment());
            model.setMockNum(entity.getMockNum());

            TableSchema.Field field = new TableSchema.Field();
            field.setFieldName(entity.getFieldName());
            field.setFieldType(entity.getFieldType());
            field.setFieldName(entity.getFieldName());
            field.setFieldType(entity.getFieldType());
            String defaultValue = entity.getDefaultValue();
            if (StringUtils.isNotBlank(defaultValue) && !"NULL".equals(defaultValue.toUpperCase(Locale.ROOT))) {
                field.setDefaultValue(entity.getDefaultValue());
            }
            field.setNotNull(entity.isNotNull());
            field.setComment(entity.getComment());
            field.setPrimaryKey(entity.isPrimaryKey());
            field.setAutoIncrement(entity.isAutoIncrement());
            field.setMockType(entity.getMockType());
            field.setMockParams(entity.getMockParams());
            field.setOnUpdate(entity.getOnUpdate());
            fieldList.add(field);
        }
        return model;
    }

    public TableSchema excelToModel(List<TableSchemaExcel> list) {
        TableSchema model = new TableSchema();
        List<TableSchema.Field> fieldList = new ArrayList<>();
        model.setFieldList(fieldList);
        for (TableSchemaExcel excel : list) {
            model.setDbName(excel.getDbName());
            model.setTableName(excel.getTableName());
            model.setTableComment(excel.getTableComment());
            model.setMockNum(excel.getMockNum());

            TableSchema.Field field = new TableSchema.Field();
            field.setFieldName(excel.getFieldName());
            field.setFieldType(excel.getFieldType());
            field.setFieldName(excel.getFieldName());
            field.setFieldType(excel.getFieldType());
            String defaultValue = excel.getDefaultValue();
            if (StringUtils.isNotBlank(defaultValue) && !"NULL".equals(defaultValue.toUpperCase(Locale.ROOT))) {
                field.setDefaultValue(excel.getDefaultValue());
            }
            field.setNotNull(excel.isNotNull());
            field.setComment(excel.getComment());
            field.setPrimaryKey(excel.isPrimaryKey());
            field.setAutoIncrement(excel.isAutoIncrement());
            field.setMockType(excel.getMockType());
            field.setMockParams(excel.getMockParams());
            fieldList.add(field);
        }
        return model;
    }
}
